/*******************************************************************************
* ???			   :??????
* ???IO	     : 
* ????       :1602????
*	??					 :
*******************************************************************************/

#include<reg51.h>
#include"lcd.h"
#include<intrins.h>
#include<stdio.h>

sbit Temp_data=P3^6;
/*******************************************************************************
* ???         : main
* ????		   : ???
* ??           : ?
* ??         	 : ?
*******************************************************************************/
unsigned int rec_dat[4];
//unsigned char data_byte;
//unsigned char rec_dat_lcd=[4];
unsigned char rec_dat_lcd0[6];
unsigned char rec_dat_lcd1[6];
unsigned char rec_dat_lcd2[6];
unsigned char rec_dat_lcd3[6];

//??
void DHT11_delay_us(unsigned char n);
void DHT11_delay_ms(unsigned int z);
void DHT11_start();
unsigned char DHT11_rec_byte();
void DHT11_receive();
void InitUART(void);
unsigned char rec_dat1[16];  

void main()
{
	
	
	//unsigned char i,j;
	unsigned char Ready_dat[13]="Ready OK!\n";
	unsigned char i;
	P1=0xf0;
	InitLcd1602();
	InitUART();
	LcdShowStr(0,0,"Humi:");
	LcdShowStr(0,1,"Temp:");
	EA = 1; 
	
	//????printf??TI???1
	ES=0;
	for(i=0 ;i<13 ;i++)	//?????????
	{
	SBUF=Ready_dat[i];
	while(!TI);
	TI=0;
	}
	
	LcdShowStr(6,0,rec_dat_lcd0);
	LcdShowStr(8,0,".");
	LcdShowStr(9,0,rec_dat_lcd1);
	LcdShowStr(10,0," %");
	
	//??
	LcdShowStr(6,1,rec_dat_lcd2);
	LcdShowStr(8,1,".");
	LcdShowStr(9,1,rec_dat_lcd3);
	LcdShowStr(10,1," C");

	while(1)
	{
		DHT11_delay_ms(150);
		DHT11_receive();

		sprintf(rec_dat_lcd0,"%d",rec_dat[0]);
		sprintf(rec_dat_lcd1,"%d",rec_dat[1]);
		sprintf(rec_dat_lcd2,"%d",rec_dat[2]);
		sprintf(rec_dat_lcd3,"%d",rec_dat[3]);
		//DHT11_delay_ms(100);
		//Lcd1602_Write_Cmd(0x80);//???????? //DB7????

		LcdShowStr(6,0,rec_dat_lcd0);
		LcdShowStr(8,0,".");
		LcdShowStr(9,0,rec_dat_lcd1);
		LcdShowStr(10,0," %");
		
		//??
		LcdShowStr(6,1,rec_dat_lcd2);
		LcdShowStr(8,1,".");
		LcdShowStr(9,1,rec_dat_lcd3);
		LcdShowStr(10,1," C");


		rec_dat1[0]='H';
		rec_dat1[1]='u';
		rec_dat1[2]='m';
		rec_dat1[3]='i';
		rec_dat1[4]=':';
		rec_dat1[5]=rec_dat_lcd0[0];
		rec_dat1[6]=rec_dat_lcd0[1];
		rec_dat1[7]='\n';
		rec_dat1[8]='T';
		rec_dat1[9]='e';
		rec_dat1[10]='m';
		rec_dat1[11]='p';
		rec_dat1[12]=':';
		rec_dat1[13]=rec_dat_lcd2[0];
		rec_dat1[14]=rec_dat_lcd2[1];
		rec_dat1[15]='\n';
		
	 for(i=0 ;i<16 ;i++)	//?????????
	 {
		SBUF=rec_dat1[i];
		while(!TI);
		TI=0;
	 }
		//????????????
		

		
		
		
	}
}



//DHT11????

void DHT11_start()	
{
	Temp_data=1;
	
	DHT11_delay_us(2);
	
	Temp_data=0;
	
	DHT11_delay_ms(20);
	
	Temp_data=1;
	
	DHT11_delay_us(13);
	

}

//??????


unsigned char DHT11_rec_byte()
{
	
	unsigned char i,dat;

	
	for(i=0;i<8;i++)
	{
			while(!Temp_data);
		  DHT11_delay_us(8);
			dat<<=1;
			if(Temp_data==1)
			{
				dat+=1;
			}
			while(Temp_data);
	}

	return dat;

	
}


//???????
void DHT11_receive()
{
	unsigned int R_H,R_L,T_H,T_L;
	unsigned char RH,RL,TH,TL,revise;
	
	DHT11_start();
	Temp_data=1;
	if(Temp_data==0)
	{
		while(Temp_data==0);   //????     
        DHT11_delay_us(40);  //?????80us
		
        R_H=DHT11_rec_byte();    //???????  
        R_L=DHT11_rec_byte();    //???????  
        T_H=DHT11_rec_byte();    //???????  
        T_L=DHT11_rec_byte();    //???????
        revise=DHT11_rec_byte(); //?????

        DHT11_delay_us(25);    //??

        if((R_H+R_L+T_H+T_L)==revise)      //??
        {
            RH=R_H;
            RL=R_L;
            TH=T_H;
            TL=T_L;
	
        } 
        /*????,????*/
        rec_dat[0]=RH;
        rec_dat[1]=RL;
        rec_dat[2]=TH;
        rec_dat[3]=TL;
				


	}
	
}

//??us   --2*n+5us
void DHT11_delay_us(unsigned char n)
{
    while(--n);
}

//??ms
void DHT11_delay_ms(unsigned int z)
{
   unsigned int i,j;
   for(i=z;i>0;i--)
      for(j=110;j>0;j--);
}


void InitUART(void)//?????1??????????
{
	EA = 1;
	ES = 1;
	SCON=0x40;					//????????1
	REN=1;						//????
	TMOD=0x20;					//???1?????2
	TH1=0xFd,TL1=0xFd;		
	TI=1;                       //???????
	TR1=1;
}

